﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace Sunny.UI.Demo
{
    public partial class FButton : UIPage
    {
        public FButton()
        {
            InitializeComponent();
            uiToolTip1.SetToolTip(uiButton1, uiButton1.Text);
           
        }

        /// <summary>
        /// 放在 [窗体Load (NeedReload = true)] 的内容每次页面切换，进入页面都会执行。
        /// 这三个选一个用就行了。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FButton_Load(object sender, EventArgs e)
        {
            Console.WriteLine("1. FButton_Load");

        }

        //放在 [窗体Load (NeedReload = true)] 的内容每次页面切换，进入页面都会执行。
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Console.WriteLine("3. FButton_OnLoad");
        }

        //放在 [重载Init] 的内容每次页面切换，进入页面都会执行。
        public override void Init()
        {
            base.Init();            

            Console.WriteLine("2. FButton_Init");
        }

        //放在 [Final] 的内容每次页面切换，退出页面都会执行
        public override void Final()
        {
            base.Final();
            Console.WriteLine("4. FButton_Final");
        }

        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            
        }

        private void uiSymbolButton25_Click(object sender, EventArgs e)
        {
            Frame.SelectPage(5000);
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            //传值给页面1001
            //设置FAvatar的Label文字
            SendParamToPage(1001, "你好");
            if (this.ShowAskDialog2(CodeTranslator.Current.Quit, true))
            {
                this.ShowSuccessTip(CodeTranslator.Current.PressedOK);
            }
            else
            {
                this.ShowErrorTip(CodeTranslator.Current.PressedCancel);
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            //传值给框架
            SendParamToFrame("传值给框架");
        }

        private void uiSwitch1_ActiveChanging(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !this.ShowAskDialog("您确认要改变当前开关的状态吗？");
        }

        private void uiButton1_Click_1(object sender, EventArgs e)
        {
           string UpdateTime = DateTime.Now.ToString("G");
            uiLabel1.Text = "最后更新：" + UpdateTime;
        }

        private void uiButton2_Click_1(object sender, EventArgs e)
        {
            Task.Factory.StartNew(PlcHeartBeat, TaskCreationOptions.LongRunning);
        }
        private void PlcHeartBeat()
        {
            ushort plcConnect = 0;
            int retry = 0;
            int count = 0;
            while (true)
            {
                try
                {
                    if (true)
                    {
                        //if (PLCModbusTcp.WriteRegisters(_Settings.plcParam.StationNum, _Settings.plcParam.PLCAddress[0].MW5800, new ushort[] { plcConnect }))
                        //{
                        //    retry = 0;
                        //    PLCKeepAlive = true;
                        //}
                        //else
                        //{
                        //    if (++retry == 10 && PLCKeepAlive)
                        //    {
                        //        Machine.Instance.ErrorMessage = "[PlcHeartBeat]: PLC心跳发送失败";
                        //        Machine.Instance.plcConnect = false;
                        //    }
                        //}
                        plcConnect = ++plcConnect > 1 ? (ushort)0 : (ushort)1;
                        Console.WriteLine(DateTime.Now.ToString("D2")+"[PlcHeartBeat]: PLC心跳发送成功，心跳值：" + plcConnect );
                        if (++count >= 10)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //ColoredOutput.WriteLine(PresetColor.Red, "[PlcHeartBeat]: 执行PLC心跳操作异常: " + ex.Message);
                }
                Thread.Sleep(500);
            }
        }
    }
    public class CodeTranslator : IniCodeTranslator<CodeTranslator>
    {
        public string Default { get; set; } = "默认信息提示框";
        public string Confirm { get; set; } = "确认信息提示框";
        public string PressedOK { get; set; } = "您点击了确定按钮";
        public string PressedCancel { get; set; } = "您点击了取消按钮";
        public string Success { get; set; } = "正确信息提示框";
        public string Warning { get; set; } = "警告信息提示框";
        public string Error { get; set; } = "错误信息提示框";
        public string Loading { get; set; } = "数据加载中";
        public string InputString { get; set; } = "请输入字符串";
        public string PressedNotifier { get; set; } = "嗨，你点击了右下角的弹窗消息";
        public string LoginSuccess { get; set; } = "登录成功";
        public string Prepare { get; set; } = "准备开始";
        public string OpenDir { get; set; } = "打开文件夹";
        public string Quit { get; set; } = "Exit？";
    }
}
